package com.sunwayworld.basemodule.framework.resource.impl;

import com.sunwayworld.basemodule.framework.resource.ThirdPushResource;
import com.sunwayworld.basemodule.samplemanage.sample.bean.LimsSampleBean;
import com.sunwayworld.basemodule.samplemanage.sample.service.LimsSampleService;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import com.sunwayworld.framework.spring.annotation.GikamBean;
import com.sunwayworld.framework.utils.CollectionUtils;
import com.sunwayworld.framework.utils.StringUtils;
import com.sunwayworld.module.item.file.bean.CoreFileBean;
import com.sunwayworld.module.item.file.service.CoreFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@GikamBean
public class ThirdPushResourceImpl implements ThirdPushResource {

    @Autowired
    @Lazy
    private CoreFileService coreFileService;

    @Autowired
    @Lazy
    private LimsSampleService sampleService;

    private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");

    @Override
    public void postFile(String businessCode, String ids, String base64, String bizCategory) {
        // 如果bizCategory为analyte，则为实验结果录入-样本单独拍照图片上传
        if(StringUtils.equalsIgnoreCase("analyte", bizCategory)) {
            byte[] decode = Base64.getDecoder().decode(base64);
            LocalDateTime now = LocalDateTime.now();
            // 样本id
            List<Long> idList = Arrays.asList(ids.split(","))
                .stream().map(Long::parseLong).collect(Collectors.toList());
            // 查询所有样本信息，每一个样本编号下仅有一个检测项目
            List<LimsSampleBean> samples = sampleService.selectListByFilter(SearchFilter.instance()
                .match("id", idList).filter(MatchPattern.OR));
            if (CollectionUtils.isEmpty(samples)) {
                return;
            }
            for (LimsSampleBean sample: samples) {
                String hissampleCode = !StringUtils.isEmpty(sample.getHissampleCode())?sample.getHissampleCode():"";
                String productCode = !StringUtils.isEmpty(sample.getExt$Item("productcode"))
                    ?sample.getExt$Item("productcode"):"";
                String sampleName = !StringUtils.isEmpty(sample.getSampleName())?sample.getSampleName():"";
                String fileName = hissampleCode + "-" + productCode + "-"
                    + sampleName + "-" + now.format(formatter) + ".jpg";
                MultipartFile file = new MockMultipartFile(fileName, fileName, "image/pjpeg", decode);
                CoreFileBean coreFile = new CoreFileBean();
                coreFile.setTargetId(businessCode + "$" + String.valueOf(sample.getId()));
                coreFile.setBizCategory(bizCategory);
                coreFileService.upload(coreFile, file);
            }
        } else {
            // 样本接收等其他页面
            byte[] decode = Base64.getDecoder().decode(base64);
            LocalDateTime now = LocalDateTime.now();
            String[] idList = ids.split(",");
            for (String id : idList) {
                String fileName = now.format(formatter) + ".jpg";
                MultipartFile file = new MockMultipartFile(fileName, fileName, "image/pjpeg", decode);
                CoreFileBean coreFile = new CoreFileBean();
                coreFile.setTargetId(businessCode + "$" + id);
                coreFile.setBizCategory(bizCategory);
                coreFileService.upload(coreFile, file);
            }
        }
    }

}
